const cloud = require('wx-server-sdk')

cloud.init()

const SUCCESS = 0;
const BAD_PARAM = -1;
const NOT_FOUND = -2;

/// (id: BookId) => (code: StatusCode, result?: Book)
exports.main = async (event) => {
  const id = event.id;
  
  if (id == null) {
    return {
      code: BAD_PARAM
    };
  }

  const db = cloud.database();
  try {
    const book = await db.collection('books').doc(id).get();
    return {
      code: SUCCESS,
      result: book
    };
  } catch (e) {
    return {
      code: NOT_FOUND
    };
  }
}
